import { Component, ReactNode } from 'react'
import classnames from 'classnames';
import { View } from '@tarojs/components';
import Taro from '@tarojs/taro'
const getCurrentInstance = Taro.getCurrentInstance;
type PageInstance = Taro.PageInstance;
import deepClone from 'lodash/cloneDeep'
type RouterInfo = Taro.RouterInfo;
import IQRequest from '@common/iq-request/request'



export interface BaseProps<T = {}> {
    /**
     * 自定义class
     */
    className?: string;
    /**
     * 自定义样式
     */
    style?: React.CSSProperties;
    action?: {};
    state?: T;
    children?: any;
}

export interface BaseState {
    loading?: boolean;
}
type ClassValue = string | number | undefined | null | boolean | { [key: string]: boolean };

export class BasePage<TProps = {}, TState = {}, TStore = {}> extends Component<
    BaseProps<TStore> & TProps,
    TState & BaseState
> {
    constructor(props, log?: any) {
        super(props);
    }


    get $router() {
        return deepClone(getCurrentInstance().router || ({} as RouterInfo));
    }

    componentWillMount() {
    }

    componentDidMount() {
        window.addEventListener('scroll', this.onPageScroll)
    }

    componentWillUnmount() {
        window.removeEventListener('scroll', this.onPageScroll);
    }

    onPageScroll(e) { }
    classNames(...args: ClassValue[]) {
        return classnames(args);
    }

    className(...args: ClassValue[]) {
        return this.classNames(...args.concat([this.props.className]));
    }
    onSetHistoryList(data) {
        let list = Taro.getStorageSync('searchHistoryList') || []
        if (!list.includes(data)) {
            list.push(data)
            Taro.setStorageSync('searchHistoryList', list)
        }
    }
    Toast(title, icon, duration = 3000) {
        Taro.showToast({
            title: title,
            icon: icon || 'loading',
            duration: duration
        })
    }
    HidToast() {
        Taro.hideToast()
    }

    wxShareCount = (options, rep) => {
        IQRequest.VIVORequest(options.url, options.params).then((res: any) => {
            rep(res)
        })
    }
    wxCheckedLogin = () => {
        let status = Taro.getStorageSync('Authorization')
        return !!status
    }

}
